//
//  MyConnection.m
//  婚礼记
//
//  Created by 月月 on 15/4/6.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MyConnection.h"
#import "Reachability.h"
#import "Tools.h"
@implementation MyConnection

- (void)dealloc
{
    [_requestUrl release];
    [_receiveData release];
    [super dealloc];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.receiveData = [NSMutableData data];
    }
    return self;
}

+ (BOOL)netWorkReachability
{
    Reachability *reach = [Reachability reachabilityWithHostName:@"http://www.baidu.com"];
    NetworkStatus status = [reach currentReachabilityStatus];
    switch (status) {
        case NotReachable:
        {
            
            return NO;
        }
            break;
        case ReachableViaWiFi:
        {
            
            return YES;
        }
            break;
        case ReachableViaWWAN:
        {
            
            return YES;
        }
            break;
            
        default:
            break;
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {// 点击的是确定
        
    } else { // 点击的是取消
        
    }
}







+ (void)connectWithUrl:(NSString *)urlStr parmaters:(NSDictionary *)parmaters delegate:(id<MyConnectionDelegate>) delegate type:(NSInteger)type
{
    BOOL judge = [MyConnection netWorkReachability];
    
    if (judge) { // 有网
        
        MyConnection *conn = [[MyConnection alloc] init];
        conn.myType = type;
        conn.delegate = delegate;
        [conn startWithUrl:urlStr parmaters:parmaters];
        [conn release];
        
    } else {  // 没网
        
        NSString *url = [MyConnection urlWithName:urlStr parmaters:parmaters];
        
        NSData *data = [Tools readWithName:url directory:NSLibraryDirectory];
        
        [delegate receiveDataFinished:data type:type];
        
    }
    
}


+ (NSString *)urlWithName:(NSString *)urlStr parmaters:(NSDictionary *)parmaters
{
    NSString *par = @"";
    for (NSString *key in [parmaters allKeys]) {
        if ([par length] == 0) {
            par = [NSString stringWithFormat:@"?%@=%@", key, [parmaters objectForKey:key]];
        } else {
            par = [NSString stringWithFormat:@"%@&%@=%@", par, key, [parmaters objectForKey:key]];
        }
    }
    urlStr = [NSString stringWithFormat:@"%@%@", urlStr, par];
    return urlStr;
}


// GET网络请求
// 拼接URL
- (void)startWithUrl:(NSString *)urlStr parmaters:(NSDictionary *)parmaters
{
    
    urlStr = [MyConnection urlWithName:urlStr parmaters:parmaters];
    
    // 不能有中文和空格,把空格和汉子全部转换成UTF8
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    self.requestUrl = urlStr;
    
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    [NSURLConnection connectionWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receiveData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [self.delegate receiveDataFinished:self.receiveData type:self.myType];
    
    [Tools writeWithName:self.requestUrl data:self.receiveData directory:NSLibraryDirectory];
    
    
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"网络请求错误 ======== %@", error);
}










@end

